<template>
  <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body @close="closeDialog">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="版本编码" prop="versionsCode">
        <el-input v-model="form.versionsCode" placeholder="请输入版本编码" />
      </el-form-item>
      <el-form-item label="版本名称" prop="versionsName">
        <el-input v-model="form.versionsName" placeholder="请输入版本名称" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
      </el-form-item>
    </el-form>
    <el-tabs type="border-card" v-if="processList.length > 0 && form.versionsId != null" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane v-for="(process, index) in processList" :key="index" :label="process.processName" :name="'productbom' + index">
        <RouteproductMain ref="productbom" :routeId="form.routeId" :recordId="form.recordId" :versionsId="form.versionsId" :processName="process.processName" :processCode="process.processCode" :processId="process.processId" :optType="optType" :bomItemCode="bomItemCode" :itemId="itemId" :workorderId="workorderId" @backEndData="getSubassembly" :subassemblyList="subassemblyList">
        </RouteproductMain>
      </el-tab-pane>
    </el-tabs>
    <div style="padding: 10px;">
      审核信息：<el-switch :active-value="1" v-model="form.auditStatus" :inactive-value="0" active-text="通过" inactive-text="不通过"></el-switch>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="cancel" v-if="optType == 'view'">返回</el-button>
      <el-button type="primary" @click="submitForm" v-else>确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import {
  listRouteproduct
} from "@/api/mes/pro/routeproduct";

import {
  listVersions,
  getProcesses,
  updateVersions,
} from "@/api/system/versions";

import RouteproductMain from "../../proroute/components/routeproductbom.vue";
export default {
  name: "Audit",
  dicts: ["sys_yes_no"],
  components: { RouteproductMain },
  data() {
    return {
      processList: [],
      title: "工艺路线审核",
      open: false,
      optType: "edit",
      //bom版本编号
      bomItemCode: "",
      //bom版本id
      itemId: "",
      recordId: "",
      form: {
        auditStatus: 0,
      },
      //子组件传过来的数据
      subassemblyList: [],
      autoGenFlag: false,
      typeList: [],
      rules: {
        versionsCode: [
          { required: true, message: "版本编码不能为空", trigger: "blur" },
        ],
        versionsName: [
          { required: true, message: "版本名称不能为空", trigger: "blur" },
        ],
        // enableFlag: [
        //   { required: true, message: "是否启用不能为空", trigger: "blur" },
        // ],
      },
      workorderId: null,
      activeName: 'productbom0'
    }
  },
  methods: {
    //获取子组件数据
    getSubassembly(msg, type) {
      if (type == "add") {
        msg.forEach(item => {
          if (item != undefined) {
            this.subassemblyList.push(item);
          }
        });
        console.log(this.subassemblyList, "祖父组件数据");
      } else if (type == "del") {
        this.subassemblyList = this.subassemblyList.filter(
          _ => _.itemCode != msg.itemCode
        );
        console.log(this.subassemblyList, "祖父组件数据11111");
      }
    },
    init(row) {
      this.open = true;
      this.bomItemCode = row.bomItemCode
      this.workorderId = row.workorderId
      this.activeName = 'productbom0'
      this.form.recordId = row.recordId
      this.form.routeId = row.routeId

      console.log(row, "row");
      // , bomItemCode: row.bomItemCode
      listRouteproduct({ recordId: row.recordId }).then((response) => {
        if (response.rows.length > 0) {
          this.recordId = response.rows[0].recordId
          this.itemId = response.rows[0].itemId
          return listVersions({ recordId: response.rows[0].recordId });
        } else {
          this.$modal.msgError("工艺路线不存在");
        }
      }).then(response => {
        if (!response.rows[0]["auditStatus"]) {
          response.rows[0]["auditStatus"] = 0
        }
        this.form = response.rows[0];

        return getProcesses(this.form.routeId)
      }).then(res => {
        this.processList = res.data;
        this.$nextTick(() => {
          /* for (let i = 0; i < this.$refs.productbom.length; i++) {
            const element = this.$refs.productbom[i];
            element.init();
          } */
          if (this.$refs['productbom'] && this.$refs['productbom'].length > 0) {
            this.$refs['productbom'][0].init()
          }
        });
      });
    },
    /* 审核-工艺维护tabs */
    handleClick(tab, event) {
      this.activeName = 'productbom' + tab.index
      this.$refs['productbom'][tab.index].init()
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.processList = [];

      // this.reset();
    },
    // 表单重置
    reset() {
      this.autoGenFlag = false;
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form);
      console.log("=============");
      this.$confirm("确认审核吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$refs["form"].validate(valid => {
          if (valid) {
            // if (this.form.versionsId != null) {

            this.form.proRouteVersionsBomList = this.subassemblyList;
            this.form.bomItemCode = this.bomItemCode;
            updateVersions(this.form).then(response => {
              this.$modal.msgSuccess("审核成功");
              this.open = false;
              this.getList();
            });
            // }
          } else {
            this.$modal.msgError("请核验必填项");
          }
        });
      });
    },
    /* 关闭时销毁组件 */
    closeDialog() {
      console.log("productBom length ", this.$refs.productbom.length)
      for (let i = 0; i < this.$refs.productbom.length; i++) {
        const element = this.$refs.productbom[i];
        console.log("element", element)
        element && element.$destroy();
      }
    }
  }
}
</script>